// ignore_for_file: dangling_library_doc_comments, slash_for_doc_comments
/*** 
 * @Author: Zhong
 * @Date: 2025-03-28 14:08:00
 * @LastEditors: Zhong zhongxiaodong@eenet.com
 * @LastEditTime: 2025-05-15 15:24:39
 * @FilePath: /flutter_demo/lib/common_code/widgets/custom_checkbox.dart
 * @Description: 复选框
 */

import 'package:flutter/material.dart';

class CustomCheckbox extends StatefulWidget {
  final bool value;
  final ValueChanged<bool> onChanged;
  final Color activeColor;
  final double borderWidth;
  final bool isRound;
  final double borderRadius;
  final double size;

  const CustomCheckbox({
    super.key,
    required this.value,
    required this.onChanged,
    this.activeColor = Colors.blue,
    this.borderWidth = 2.0,
    this.isRound = false,
    this.borderRadius = 4.0,
    this.size = 24.0,
  });

  @override
  _CustomCheckboxState createState() => _CustomCheckboxState();
}

class _CustomCheckboxState extends State<CustomCheckbox>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 200),
    );
    _animation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: _controller,
        curve: Curves.easeInOut,
      ),
    );
    if (widget.value) {
      _controller.forward();
    }
  }

  @override
  void didUpdateWidget(CustomCheckbox oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.value != oldWidget.value) {
      if (widget.value) {
        _controller.forward();
      } else {
        _controller.reverse();
      }
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        widget.onChanged(!widget.value);
      },
      child: AnimatedBuilder(
        animation: _animation,
        builder: (context, child) {
          return Container(
            width: widget.size,
            height: widget.size,
            decoration: BoxDecoration(
              shape: widget.isRound ? BoxShape.circle : BoxShape.rectangle,
              borderRadius: widget.isRound
                  ? null
                  : BorderRadius.circular(widget.borderRadius),
              border: Border.all(
                color: widget.value
                    ? widget.activeColor
                    : Theme.of(context).unselectedWidgetColor,
                width: widget.borderWidth,
              ),
              color: widget.value
                  ? widget.activeColor.withOpacity(_animation.value)
                  : null,
            ),
            child: Opacity(
              opacity: _animation.value,
              child: Center(
                child: Icon(
                  Icons.check,
                  size: widget.size * 2 / 3,
                  color: Colors.white,
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
